package net.lesson.sample.client;

import net.lesson.sample.client.bean.ServerInfo;
import net.lesson.sample.foo.Foo;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * 对服务器进行压力测试
 */

public class ClientTest {
    private static boolean done = false;

    public static void main(String[] args) throws IOException {
        File cachePath = Foo.getCacheDir("client/test");
        ServerInfo info = UDPSearcher.searchServer(10000);
        System.out.println("Server:" + info);

        if (info == null) {
            return;
        }

        // 当前连接数量
        int size = 0;
        final List<TCPClient> tcpClients = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            try {
                TCPClient tcpClient = TCPClient.startWith(info, cachePath);
                if (tcpClient == null) {
                    System.out.println("连接异常");
                    continue;
                }
                tcpClients.add(tcpClient);
                System.out.println("连接成功：" + (++size));
            } catch (IOException e) {
                System.out.println("连接异常");
            }
            try {
                Thread.sleep(20);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }


        Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {
                while (!done) {
                    for (TCPClient tcpClient : tcpClients) {
                        tcpClient.send("Hello~");
                    }
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        });
        thread.start();

        System.in.read();
        done = true;
        // 等待线程结束
        try {
            thread.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        // 客户端退出操作
        for (TCPClient tcpClient : tcpClients) {
            tcpClient.exit();
        }
    }
}
